$(window).load(function() {

	    var a = $("#addsite1").validate({
		rules: {
			site_title: "required",
			site_url:  { 
				required:true,
				url:true
			},
			host_username: "required",
			host_psw: "required",
			host_address: {
                    required: true,
                    email: true
            },
			database_url: { 
				required:true,
				url:true
			},
			database_name: "required",
			database_username: "required",
			database_psw: "required",			
			ftp_dir:  { 
				required:true,
				url:true
			},
			ftp_user: "required",
			ftp_psw:  "required"			
		},
		messages: {
			site_title: "请输入网站标题",
			site_url: {
				required: "请输网站地址",
				url:"您输入的URL格式不正确"		
			},
			host_username: "请输入管理员账号",
			host_psw: "请输入管理员密码",
			host_address:{
                    required: "请输入邮箱",
                    email: "您输入的邮箱格式不对"
            },
			database_url: {
				required: "请输入数据库地址",
				url:"您输入的URL格式不正确"		
			},
			database_name: "请输入数据库名称",
			database_username: "请输入用户名",
			database_psw: "请输入数据库密码",
			
			ftp_dir: {
				required: "请输入FTp地址",
				url:"您输入的URL格式不正确"		
			},
			ftp_user: "请输入FTP用户名",
			ftp_psw: "请输入FTP密码"	
		},			
		invalidHandler: function(d, b) {
			var e = b.numberOfInvalids();
			if (e) {
				var c = e == 1 ? "您有 1 处还没有填.": "您有 " + e + " 处还没有填.";
				$(".box .content").removeAlertBoxes();
				$(".box .content").alertBox(c, {
					type: "warning",
					icon: true,
					noMargin: false
				});
				$(".box .content .alert").css({
					width: "",
					margin: "0",
					borderLeft: "none",
					borderRight: "none",
					borderRadius: 0
				})
			} else {
				$(".box .content").removeAlertBoxes()
			}
		},
		showErrors: function(c, d) {
			this.defaultShowErrors();
			var b = this;
			$.each(d,
			function() {
				var f = $(this.element);
				var e = f.parent().find("label.error").hide();
				e.addClass("red");
				e.css("width", "");
				f.trigger("labeled");
				e.fadeIn()
			})
		},
		submitHandler:function(form){
			$('#addsite1').ajaxSubmit(createsite1());
		 }
	})
		
	$('#addsite1 input').keydown(function(e)
	{
			if (e.keyCode == 13) {
				$('#addsite1').submit();
			}
	})	
})

function createsite1(){
		var url = $('#install').val();
		$.ajax({
			url:'./doAddSite',
			type: 'POST',
			data:'host_username='+$('#host_username').val()+'&host_psw='+$('#host_psw').val()+'&host_address='+$('#host_address').val()+'&database_url='+$('#database_url').val()+'&database_name='+$('#database_name').val()+'&database_username='+$('#database_username').val()+'&database_psw='+$('#database_psw').val()+'&ftp_dir='+$('#ftp_dir').val()+'&ftp_user='+$('#ftp_user').val()+'&ftp_psw='+$('#ftp_psw').val()+'&install='+$('#install').val()+'&site_url='+$('#site_url').val()+'&site_title='+$('#site_title').val(),
			dataType: 'html',
			timeout: 100000,
			error: function(){
				alert('Error loading XML document');
			},
			success: function(html){
				
				if(html == 2)
				{						
					$.fallr({
						content: "请选择网站安装路径！",
						icon: "error"
					})		
				}
				else
				{
					$.fallr("show", {
					buttons: {
						button1: {
							text: "是",
							danger: true,
							onclick: function(){
								window.location='./addSite2';	
							}
						},
						button2: {
							text: "否",
							onclick: function() {
								$.fallr("hide")
							}
						}
					},
					content: "<p>您确定要安装到<span style='font-weight:bold;'>'"+url+"'</span>这个目录下吗?确定请点击'是' 进行下一步操作！</p>",
					icon: "error"
					})
				}

			}
	});	
	
}

function clearform(){
	$('form').find(':text,:password').each(function() {
		$(this).attr('value','')
    });
}